package com.lvs.zmwz.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lvs.zmwz.auth.StpUserUtil;
import com.lvs.zmwz.entity.Pinglun;
import com.lvs.zmwz.entity.Shop;
import com.lvs.zmwz.entity.SysManager;
import com.lvs.zmwz.service.IPinglunService;
import com.lvs.zmwz.service.IShopService;
import com.lvs.zmwz.service.ISysManagerService;
import com.lvs.zmwz.util.IdentityUtil;
import com.lvs.zmwz.util.QPage;
import com.lvs.zmwz.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-21
 */
@RestController
@RequestMapping("/api/front/pinglun")
public class AppPinglunController {

    @Autowired
    private IPinglunService iPinglunService;

    @Autowired
    private IShopService iShopService;

    @Autowired
    private ISysManagerService iSysManagerService;

    /**
     * 评论列表
     * @return
     */
    @GetMapping("/select")
    public ResultView select(String shopId) {
        QueryWrapper<Pinglun> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("shop_id",shopId);
        List<Pinglun> pinglunList=this.iPinglunService.list(queryWrapper);
        return ResultView.ok(pinglunList);
    }


    /**
     * 用户评论
     * @return
     */
    @PostMapping("/add")
    public ResultView add(Pinglun pinglun) {
        String managerId= StpUserUtil.getLoginIdAsString();
        SysManager sysManager=this.iSysManagerService.getById(managerId);
        Shop shop=this.iShopService.getById(pinglun.getShopId());
        pinglun.setManagerId(managerId);
        pinglun.setManagerName(sysManager.getMName());
        pinglun.setShopName(shop.getShopName());
        pinglun.setCtime(new Date());
        pinglun.setPlid(IdentityUtil.identityId("PL"));
        boolean isOk=this.iPinglunService.save(pinglun);
        if(isOk){
            shop.setPinlunNum(shop.getPinlunNum()+1);
            this.iShopService.updateById(shop);
            return ResultView.ok();
        }else{
            return ResultView.error("评论失败");
        }
    }

}
